package top.rainf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import top.rainf.constants.AttributeConstants;
import top.rainf.domain.vo.Post;
import top.rainf.service.INoticeService;
import top.rainf.support.Result;
import top.rainf.domain.vo.Notice;
import top.rainf.support.TokenGenerator;
import top.rainf.util.PagerUtil;

import java.util.List;

/**
 * @author 星环
 * @date Wed May 10 21:04:41 CST 2023
 * @description 通知的控制器类 由本人的生成器自动生成~
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    INoticeService noticeService;
    /**
    * 获取全部通知的列表
    * @return 返回是否获取成功~
    */
    @RequestMapping("/list")
    public Result list(){
        List<Notice> list = noticeService.getNoticeList();
        if(list == null){
            return Result.fail();
        }
        return Result.success().addObj("list",list);
    }

    @RequestMapping("haveNew")
    public Result haveNew(int id){
        int newCount = noticeService.checkNew(id);
        return Result.success().addObj("count",newCount);
    }



    /**
    * 根据id获取一个通知
    * @return 返回是否获取成功~
    */
    @RequestMapping("/one")
    public Result getOneNotice(int id){
        Notice notice = noticeService.getOneById(id);
        if(notice == null){
                    return Result.fail();
        }
        return Result.success().addObj("data",notice);
    }
    /**
    * 根据id删除一个通知
    ---------------------------
    * @param id 要删除的通知的id
    * @return 返回是否获取成功~
    */
    @RequestMapping("/deleteById")
    public Result deleteById(int id){
        boolean suc = noticeService.deleteOneById(id);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("/deleteByIds")
    public Result deleteByIds(List<Integer> idList){
        boolean suc = noticeService.deleteAll(idList);
        return suc ? Result.success() : Result.fail();
    }

    @RequestMapping("update")
    public Result update(Notice notice){
        boolean suc = noticeService.updateById(notice);
        return suc ? Result.success() : Result.fail();
    }

    /**
     * 用户个人中心用
     * @param nowPage
     * @param numPerPage
     * @param token
     * @return
     */
    @RequestMapping("simpleMsgList")
    public Result simpleMsgList(int nowPage,int numPerPage,@SessionAttribute(AttributeConstants.LOGIN_TOKEN)String token){
        int userId = TokenGenerator.getUserIdFromToken(token);
        if(userId == -1){
            return Result.fail("您还未登录！");
        }
        List<Notice> noticeList = noticeService.getUserSimplePostListPaged(userId,nowPage,numPerPage);
        int total = noticeService.getTotalCountOfUser(userId);
        List<String> pageList = PagerUtil.getPageListStr(numPerPage,nowPage,total);

        return Result.success()
                .addObj("postList",noticeList)
                .addObj("pageList",pageList);
    }
}
